package cn.hznc.controller.service.order.logic;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.vo.CraftParamVo;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Administrator
 * @Date 2023/3/21 17:35
 */
@Getter
@Setter
public class OrderCraftLogic {
    private final OrderCraftDo orderCraftDo;

    private final List<CraftParamVo> updateList;

    private final List<CraftParamVo> originList;

    public OrderCraftLogic(List<CraftParamVo> originList, List<CraftParamVo> updateList) {
        this.updateList = updateList;
        this.originList = originList;
        this.orderCraftDo=new OrderCraftDo();
    }

    /**
     * 构建业务模型
     *
     * @return
     */
    public void buildOrderCraftDo() {
        // 清空业务模型
        if (CollectionUtil.isEmpty(updateList)){
            orderCraftDo.addAllElement();
            return;
        }
        // 只需要更新
        if (CollectionUtil.isEmpty(originList)){
            orderCraftDo.addCreateElement(updateList);
            orderCraftDo.addAllElement();
            return;
        }
        // 比对code判断，对删除，修改，新增
        List<String> codeList = originList.stream().map(CraftParamVo::getCode).collect(Collectors.toList());

        for (CraftParamVo craftParamVo : updateList) {
            // 1.更新的code为空
            if (!codeList.contains(craftParamVo.getCode())){
                orderCraftDo.addCreateElement(craftParamVo);
            }else {
                orderCraftDo.addUpdateElement(craftParamVo);
            }
        }
    }

    /**
     * 获取处理后的业务模型
     *
     * @return subProgramDo
     */
    public OrderCraftDo getOrderCraftDo() {
        return orderCraftDo;
    }
}
